package com.ljh.blog.common.interceptor;

import com.ljh.blog.common.constant.Constants;
import com.ljh.blog.common.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author: Themberfue
 * @date: 2025/4/2 20:28
 * @description:
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(Constants.USER_TOKEN_HEADER_KEY);
        log.info("从header中获取token：{}", token);
        if (token == null || JwtUtils.parseToken(token) == null) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
